package edu.washington.cs.sounddetector.server.api.object;

import java.io.Serializable;

import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

/**
 * <p>
 * A PeakEvent describes a time, magnitude, and duration of a peak in a sound
 * </p>
 * 
 * @author the.dylan.price@gmail.com
 * 
 */
public class PeakEvent implements Serializable
{
    private static final long serialVersionUID = 3069543906416454002L;

    /**
     * @serial
     */
    private double time;
    /**
     * @serial
     */
    private double magnitude;
    /**
     * @serial
     */
    private double duration;

    public PeakEvent()
    {
    }

    /**
     * Construct a new PeakEvent.
     * 
     * @param time
     *            the time of the peak
     * @param magnitude
     *            the magnitude of the peak from 0 to 100
     * @param duration
     *            the duration of the peak
     */
    public PeakEvent(double time, double magnitude, double duration)
    {
        Validate.isTrue(time >= 0);
        Validate.isTrue(duration >= 0);

        this.time = time;
        this.magnitude = magnitude;
        this.duration = duration;
    }

    /**
     * @return the duration
     */
    public double getDuration()
    {
        return duration;
    }

    /**
     * @param duration
     *            the duration to set
     */
    public void setDuration(double duration)
    {
        this.duration = duration;
    }

    /**
     * @param time
     *            the time to set
     */
    public void setTime(double time)
    {
        this.time = time;
    }

    /**
     * @param magnitude
     *            the magnitude to set
     */
    public void setMagnitude(double magnitude)
    {
        this.magnitude = magnitude;
    }

    /**
     * @return the time
     */
    public double getTime()
    {
        return time;
    }

    /**
     * @return the magnitude
     */
    public double getMagnitude()
    {
        return magnitude;
    }

    public String toString()
    {
        return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append("time", time)
                .append("magnitude", magnitude).append("duration", duration).toString();
    }

    @Override
    public int hashCode()
    {
        return new HashCodeBuilder().append(time).append(magnitude).append(duration).toHashCode();
    }

    @Override
    public boolean equals(Object obj)
    {
        if (obj instanceof PeakEvent)
        {
            PeakEvent other = (PeakEvent) obj;
            return new EqualsBuilder().append(time, other.time).append(magnitude, other.magnitude)
                    .append(duration, other.duration).isEquals();
        }
        else
        {
            return false;
        }
    }

}
